--
-- Created by IntelliJ IDEA.
-- User: yao.chen
-- Date: 2019-01-26
-- To change this template use File | Settings | File Templates.
-- GiftRenderForeign.lua
-- 海外版开服礼包render
--

local GoddessWishBaseRender = require("app.boss.views.renders.GoddessWishBaseRender")
local math_min = math.min
local string_format = string.format
local string_split = string.split

local ClassRef = app.mvc.createPanelClass(...)

local CAN_BUY_GIFT = L("auction_button2")
local HAS_BOUGHT = L("tips_3000411")
local MONEY_TIP_STR = L("demondShop_notenough")

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.GiftNodeFour))
    sprite:init()
    return sprite
end

function ClassRef:init()
    self.super.init(self, data)

    self._buyButtonName = self._ui["Panel/Button_buy/Text_name"]
    self._buyButton = self._ui["Panel/Button_buy/Button"]
    self._buyButton:addTouchEventListener(function(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if not self._data then return end
            local daibiCost = self:getDaibiCost()
            local buyItemDialog = display.newConfirmDialog(string_format(L("openServicePacketBuy"), daibiCost),
                function() -- 确定
                    local daibi = UD:getDaibiValue(self:getDaibiId())
                    print("UD:getDaibiValue(self:getDaibiId())", daibi, daibiCost, type(daibi))
                    if daibi >= daibiCost then
                        local params = {goodsId = self._data.id, num = 1}
                        executeFunc(self._onOperate, "buyOpenServerGift", params)
                    else
                        executeFunc(self._onOperate, "needPay", MONEY_TIP_STR)
                    end
                end)
            UIHandler:addPopDialog(buyItemDialog)
        end
    end)

    local renders = {0, 0, 0}
    for i = 1, #renders do
        local icon_panel = self._ui["icon_" .. i]
        local render = GoddessWishBaseRender.create(icon_panel:getChildByName("icon"))
        render:setTipEnable(true)
        render:setShowOrign(true)
        renders[i] = render
    end
    self._giftIconRenders = renders
end

function ClassRef:getGiftsId()
    if not self._data then return end
    return self._data.id or 0
end

function ClassRef:getDaibiId()
    if not self._cost then return end
    local ary = string_split(self._cost, "|")
    return tonumber(ary[1])
end

function ClassRef:getDaibiCost()
    if not self._data then return end
    return self._data.cost[1].num or 0
end

function ClassRef:getIsSellOut()
    if not self._data then return end
    return self._data.leftNum == 0
end

------------------------
--"<var>" = {
--    "cost" = {
--    1 = {
--          "num"  = 400
--          "type" = "coin"
--          }
--}
--"id"       = 6001001
--"itemId"   = 77019201
--"itemType" = "mount"
--"leftNum"  = 1
--"limitNum" = 1
--"numType"  = "OneLifeLimit"
--}
function ClassRef:refresh()
    if not self._data then return end
    local data = self._data
    local conf = GD:queryGoodById(data.id)
    if not conf then return end
    self._cost = conf.cost
    local items = {}
    local item = UD:parseRewardToBagItem(conf.item)     --掉落
    local dropItems = UD:parseDropIDToBagItem(conf.awardDropId)  --额外掉落
    table.insert(items, item)
    for _, v in ipairs(dropItems) do
        table.insert(items, v)
    end

    local renders = self._giftIconRenders
    local count = math_min(#renders, #items)    -- 防止越界
    for i = 1, count do
        local item = items[i]
        if item.quality then
            local icon_panel = self._ui["icon_" .. i]
            icon_panel:findChild("bg"):setColor(Enums.QualityColor[item.quality])
        end
        renders[i]:setData(item)
    end

    self._ui.tlt:setString(L(conf.name))
    self._ui.text:setString(L(conf.desc))

    if data.leftNum > 0 then
        self._buyButtonName:setString(CAN_BUY_GIFT)
        display.setBtnTouchEnable(self._buyButton, true)
    else
        self._buyButtonName:setString(HAS_BOUGHT)
        display.setBtnTouchEnable(self._buyButton, false)
    end
    local num = self._ui["Panel/Button_buy/num"]
    num:setString(data.cost[1].num)
end

return ClassRef
